//
//  SecondViewController.m
//  KeepFit
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "SecondViewController.h"
#import "JT3DScrollView.h"
#import "IngredientDetailView.h"
#import "FoodPages.h"
#import "FinalViewController.h"
@interface SecondViewController ()
@property (nonatomic, strong) JT3DScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *bigArr;
@end

@implementation SecondViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    // 去掉最上面的电池那个
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bigArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollView = [[JT3DScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.effect = JT3DScrollViewEffectDepth;
    [self.view addSubview:self.scrollView];
    
    [self handleData];
}

- (void)handleData
{
    NSString * str = [NSString stringWithFormat:@"http://food.boohee.com/fb/v1/topics/%@",self.ID];
//    NSString *str = @"http://food.boohee.com/fb/v1/topics/43";
    [NetWorkingCache getUrl:str success:^(id result) {
        NSArray *array = [[result objectForKey:@"topic"] objectForKey:@"pages"];
        for (NSDictionary *dic in array) {
            FoodPages *foodPages = [FoodPages modelWithDic:dic];
            [self.bigArr addObject:foodPages];
            [self creatViewWithpages:foodPages];
        }
    
    } failure:^(NSError *error) {
        
    }];
    
    
}
- (void)creatViewWithpages:(FoodPages *)page
{
    CGFloat width = CGRectGetWidth(self.scrollView.frame);
    CGFloat height = CGRectGetHeight(self.scrollView.frame);
    
    CGFloat x = self.scrollView.subviews.count * width;
    
    IngredientDetailView *view = [[IngredientDetailView alloc] initWithFrame:CGRectMake(x, 0, width, height)];

    [view.upImageView sd_setImageWithURL:[NSURL URLWithString:page.image_url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    view.titleLab.text = page.food_name;
    view.descriptionLab.text = page.food_description;
    [view.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view.returnButton addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:view];
    self.scrollView.contentSize = CGSizeMake(x + width, height);

    
    
    
}
- (void)buttonAction:(UIButton *)bnt
{
    FoodPages *foodPages = [self.bigArr objectAtIndex:self.scrollView.currentPage];
    NSLog(@"%@",foodPages.food_code);

    FinalViewController *finalVC = [[FinalViewController alloc] init];
    finalVC.foodCode = foodPages.food_code;
    [self.navigationController pushViewController:finalVC animated:YES];
    
    
}
- (void)returnButtonAction:(UIButton *)bnt
{
    [self.navigationController popViewControllerAnimated:NO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
